package com.jjb.www.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.gson.factory.GsonFactory;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.app.AppFragment;
import com.jjb.www.bean.UrlBean;
import com.jjb.www.http.api.AgencyListApi;
import com.jjb.www.http.api.GetAgTeachPlaybackAuthApi;
import com.jjb.www.http.api.LiveDingApi;
import com.jjb.www.http.api.MergeWatchPlaybackApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.activity.BrowserActivity;
import com.jjb.www.ui.activity.CopyActivity;
import com.jjb.www.ui.activity.VerificationRecordActivity;
import com.jjb.www.ui.adapter.LiveRecordingAdapter;
import com.jjb.www.ui.adapter.OnDemandAdapter;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;

/**
 *    author : Android CRG
 *    time   : 2018/10/18on demand
 *    desc   : 直播课程校验记录
 */
public final class LiveRecordingFragment extends AppFragment<VerificationRecordActivity> implements BaseAdapter.OnChildClickListener {
    private LiveRecordingAdapter mAdapter;
    private WrapRecyclerView mRvList;



    private static final String INTENT_KEY_ID_CARD = "id_card";
    private static final String INTENT_KEY_STU = "student_id";
    public static LiveRecordingFragment newInstance(String id_card,String student_id) {
        LiveRecordingFragment fragment = new LiveRecordingFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_ID_CARD, id_card);
        bundle.putString(INTENT_KEY_STU, student_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recording;
    }

    @Override
    protected void initView() {
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new LiveRecordingAdapter(getContext());
        mAdapter.setOnChildClickListener(R.id.tv_lock,this);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    protected void initData() {
        EasyHttp.get(this)
                .api(new LiveDingApi()
                        .setId_card(getString(INTENT_KEY_ID_CARD))
                        .setType("2")
                        .setStudent_id(getString(INTENT_KEY_STU))
                        .setPage(1)
                        .setPageSize(100))
                .request(new HttpCallback<HttpData<LiveDingApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<LiveDingApi.Bean> data) {
                        mAdapter.setData(data.getData().getData());
                    }
                });

//        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<LiveDingApi.Bean>() {
//            @Override
//            public AppAdapter<LiveDingApi.Bean> getAdapter() {
//                return mAdapter;
//            }
//
//            @Override
//            public void loadData(int p, HttpCallback<HttpListData<LiveDingApi.Bean>> callback) {
//                EasyHttp.get(getViewLifecycleOwner())
//                        .api(new LiveDingApi()
//                                .setId_card(getString(INTENT_KEY_ID_CARD))
//                                .setType("2")
//                                .setStudent_id(getString(INTENT_KEY_STU))
//                                .setPage(p)
//                                .setPageSize(100)
//                        ).request(new HttpCallback<HttpListData<LiveDingApi.Bean>>(getAttachActivity()) {
//
//                    @Override
//                    public void onStart(Call call) {
//
//                    }
//
//                    @Override
//                    public void onSucceed(HttpListData<LiveDingApi.Bean> result) {
//                        callback.onSucceed(result);
//                    }
//
//                    @Override
//                    public void onFail(Exception e) {
//                        super.onFail(e);
//                        callback.onFail(e);
//                    }
//
//                    @Override
//                    public void onEnd(Call call) {
//                        super.onEnd(call);
//                        callback.onEnd(call);
//                    }
//                });
//            }
//
//            @Override
//            public void onRefreshSuccess(List<LiveDingApi.Bean> list, int listCount) {
//
//            }
//
//            @Override
//            public void onRefreshFailure() {
//
//            }
//
//            @Override
//            public void onLoadMoreSuccess(List<LiveDingApi.Bean> loadItemList, int loadItemCount) {
//
//            }
//
//            @Override
//            public void onLoadMoreFailure() {
//
//            }
//        });
//        mRefreshView.initData();
    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {
        if (childView.getId()==R.id.tv_lock){
            EasyHttp.get(this)
                    .api(new GetAgTeachPlaybackAuthApi()
                    .setChapterCode(mAdapter.getItem(position).getChapterCode())
                    .setType("2"))
                    .request(new HttpCallback<HttpData<GetAgTeachPlaybackAuthApi.Bean>>(this) {

                        @Override
                        public void onSucceed(HttpData<GetAgTeachPlaybackAuthApi.Bean> data) {
                            getUrl(mAdapter.getItem(position).getArgsJson());
                        }
                    });
        }
    }

    private void getUrl(String argsJson) {
        UrlBean urlBean = GsonFactory.getSingletonGson().fromJson(argsJson, UrlBean.class);
        EasyHttp.get(this)
                .api(new MergeWatchPlaybackApi()
                        .setMech_id(urlBean.getMech_id())
                        .setToken(AppConfig.getInstance().getToken())
                        .setPlan_id(urlBean.getTeach_plan_id())
                        .setTask_id(urlBean.getTask_id())
                        .setType(urlBean.getType()))
                .request(new HttpCallback<HttpData<String>>(this) {

                    @Override
                    public void onSucceed(HttpData<String> data) {
                        BrowserActivity.start(getContext(),data.getData());
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        toast("还未生成回放");
                    }
                });
    }
}